using System;
using System.Configuration;
using System.IO;

namespace Drill.Core.Utilities
{
    ///<summary>
    /// Provides Configuration utility methods.
    ///</summary>
    public static class ConfigurationUtility
    {
        /// <summary>
        /// Retrieves the specified config section from the default app or web config file.
        /// </summary>
        /// <param name="configSectionName">The name of the config section to be retrieved.</param>
        /// <returns>An instance of the <see cref="ConfigurationSection"/> class.</returns>
		public static ConfigurationSection GetConfigSectionFromConfigFile(string configSectionName)
        {
            ArgumentException argumentException =
                ValidationUtility.GetArgumentExceptionForStringIsNullOrIsEmptyOrContainsOnlyWhitespace(configSectionName, "configSectionName");
            if (null != argumentException)
            {
                throw argumentException;
            }

            return ConfigurationManager.GetSection(configSectionName) as ConfigurationSection;
        }

        /// <summary>
        /// Retrieves the specified config section from the specified config file.
        /// </summary>
        /// <param name="configSectionName">The name of the config section to be retrieved.</param>
        /// <param name="filename">The filename of the config file containing the secified config section.</param>
        /// <returns>An instance of the <see cref="ConfigurationSection"/> class.</returns>
		public static ConfigurationSection GetConfigSectionFromConfigFile(string configSectionName, string filename)
        {
            ArgumentException argumentException =
                ValidationUtility.GetArgumentExceptionForStringIsNullOrIsEmptyOrContainsOnlyWhitespace(configSectionName, "configSectionName");
            if (null != argumentException)
            {
                throw argumentException;
            }

            System.Configuration.Configuration config = LoadConfigFile(filename);
            return config.GetSection(configSectionName);
        }

        ///<summary>
        /// Loads the con figuration from a .NET config file.
        ///</summary>
        ///<param name="filename">The absolute path and file name of the config file to be loaded.</param>
        ///<returns>An instance of the <see cref="Configuration" /> class loaded from the specified config file.</returns>
		public static System.Configuration.Configuration LoadConfigFile(string filename)
        {
            ArgumentException argumentException =
                ValidationUtility.GetArgumentExceptionForStringIsNullOrIsEmptyOrContainsOnlyWhitespace(filename, "filename");
            if (null != argumentException)
            {
                throw argumentException;
            }

            if (false == File.Exists(filename))
            {
                throw new FileNotFoundException(
                    string.Format("The configuration file '{0}' cannot be loaded because the file could not be found.", filename)
                  , filename
                    );
            }

            ExeConfigurationFileMap map =
                new ExeConfigurationFileMap
                    {
                        ExeConfigFilename = filename
                    };

            return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
        }
    }
}